﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EastForceCondensor.CommCtrl;
using System.Threading;

using System.Timers;

namespace MCorder.efside
{
    public class Hall
    {
        //----------------------------只读----------------------------
        //探头运行状态标记
        private bool IsRun = false;

        private CommMgmt commCtrl = new CommMgmt();

        System.Timers.Timer objTimer = new System.Timers.Timer();

        public bool isRun
        {
            get
            {
                return IsRun;
            }
        }

        public void start()
        {
            IsRun = true;
        }

        public void stop()
        {
            IsRun = false;
        }
        int i = 0;
        public void Init()
        {
            objTimer.Interval = 89; //这个时间单位毫秒,比如10秒，就写10000 
            //objTimer.Enabled = true;
            objTimer.Elapsed += new ElapsedEventHandler(objTimer_Elapsed);
            ad_current_list = new List<float>();

            if (commCtrl.Connect() == true)
            {
                Thread p = new Thread(new ThreadStart(startrecord));
                p.IsBackground = true;
                p.Start();
            }
        }
        public void Disconnect()
        {
            commCtrl.StopReceive();
            commCtrl.Disconnect();
        }

        public Hall()
        {
            IsRun = false;
        }

        public float voltage
        {
            get
            {
                return ad_current;
            }
        }


        ~Hall()
        {

        }

        public float ad_current;
        public float ad_current4;

        public List<float> ad_current_list;
        public List<float> voltage_list
        {
            get
            {
                return ad_current_list;
            }
        }

        void objTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            ADDataSet aveData = new ADDataSet();
            if (!commCtrl.GetAverageData(ref aveData, 100))
            {
                return;
            }
            //ad_current = aveData.voltageArray[0];
            else
            {
                ad_current_list.Add(aveData.voltageArray[0]);
            }
        }

        private void startrecord()
        {
            IsRun = true;           

            objTimer.Enabled = true;

            //while (IsRun)
            {
               
                //ADDataSet aveData = new ADDataSet();               
                //if (commCtrl.GetAverageData(ref aveData, 100))
                //{
                //  //  ad_current = aveData.voltageArray[0];
                //  //  ad_current4 = aveData.voltageArray[3];
                //    ad_current_list.Add(aveData.voltageArray[0]);
                //    Console.WriteLine("num="+ad_current_list.Count());

                //}
            }
        }
    }
}
